package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.AssessRecordBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 改布局重新做
 * Created by Administrator on 2017/2/27.
 */

public class AssessRecordResetAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<AssessRecordBean> datas;
    private LayoutInflater mInflater;

    private itemClickListener listener;
    private ResultSelectedListener resultListener;

    public AssessRecordResetAdapter(Context mContext, List<AssessRecordBean> datas, itemClickListener listener, ResultSelectedListener resultListener) {
        this.mContext = mContext;
        this.datas = datas;
        this.listener = listener;
        this.resultListener = resultListener;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setData(List<AssessRecordBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_assess_record_item, null);
        ViewHoderPush holder = new ViewHoderPush(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHoderPush viewholder = (ViewHoderPush) holder;
        AssessRecordBean bean = datas.get(position);

        viewholder.ll_assessrecordlist_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(position);
            }
        });
        viewholder.assess_title.setText(bean.getName());
        viewholder.assess_dsc.setText(bean.getGrade());
        if (bean.getIsAnswer().equals("0")) {//患者未答题
            viewholder.assess_status.setTextColor(mContext.getResources().getColor(R.color.light_red));
            viewholder.assess_status.setText("未答");
        } else {
            viewholder.assess_status.setTextColor(mContext.getResources().getColor(R.color.assess_status));
            viewholder.assess_status.setText("已答");
        }

        if (bean.isDisplay()) {
            viewholder.btn_assessrecordlist_select.setVisibility(View.VISIBLE);
        } else {
            viewholder.btn_assessrecordlist_select.setVisibility(View.GONE);
        }

        if ("2".equals(bean.getAnswerType())) {
            viewholder.assess_type.setText("护士测评:\t " + bean.getAnswerPerson());
        } else if ("3".equals(bean.getAnswerType())) {//患者自评
            viewholder.assess_type.setText("患者自评");
        }else {
            viewholder.assess_type.setText("");
        }
        if (!TextUtils.isEmpty(bean.getScore()))
            viewholder.assess_fraction.setText(bean.getScore() + "分");
        if (!TextUtils.isEmpty(bean.getDate())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(new Date(Long.valueOf(bean.getDate())));
            // viewholder.assess_send_time.setText("发送日期:" + date);
            viewholder.assess_send_time.setText(Html.fromHtml("<font color =\"#939393\">发送日期: \t"
                    + " </font><font color =\"#414141\">" + date + " </font>"));
        }
        if (!TextUtils.isEmpty(bean.getAnswerTime())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(new Date(Long.valueOf(bean.getAnswerTime())));
            //  viewholder.assess_send_time.setText("答题日期:" + date);
            viewholder.assess_answer_time.setText(Html.fromHtml("<font color =\"#939393\">答题日期: "
                    + " \t</font><font color =\"#414141\">" + date + " </font>"));
        }else{
            viewholder.assess_answer_time.setText("");
        }

        if (datas.get(position).isSelect()) {
            viewholder.btn_assessrecordlist_select.setBackgroundResource(R.mipmap.btn_multiselect_pre);
        } else {
            viewholder.btn_assessrecordlist_select.setBackgroundResource(R.mipmap.btn_multiselect);
        }
        Log.i("-desc-->","desc"+bean.getAnswerTimeDesc());
        if (!TextUtils.isEmpty(bean.getAnswerTimeDesc())) {
            String[] split = bean.getAnswerTimeDesc().split(",");
            if (split != null && split.length == 2) {
                viewholder.assess_hospitalized_status.setText(Html.fromHtml("<font color =\"#939393\">" + split[0]
                        + ":\t </font>" + "<font color =\"#414141\">" + split[1] + " </font>"));
            }else if(split != null && split.length == 1)
            {
                viewholder.assess_hospitalized_status.setText(split[0]);
            }
            // viewholder.assess_hospitalized_status.setText("入院:"+bean.getAnswerTimeDesc());
        }

        viewholder.btn_assessrecordlist_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (datas.get(position).isSelect()) {
                    resultListener.onSelected("no_select", position);
                } else {
                    resultListener.onSelected("select", position);
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public interface itemClickListener {
        void onItemClick(int position);
    }

    public interface ResultSelectedListener {
        void onSelected(String type, int position);
    }

    class ViewHoderPush extends RecyclerView.ViewHolder {
        public Button btn_assessrecordlist_select;//选择框
        public LinearLayout ll_assessrecordlist_root;//点击部分
        public TextView assess_title;//标题
        public TextView assess_status;//答题状态
        //  public TextView tv_assessrecordlist_doctor;//评测人
        public TextView assess_fraction;//分数
        public TextView assess_send_time;//发送日期
        public TextView assess_dsc;//功能描叙
        public TextView assess_hospitalized_status;//入院和出院时间
        public TextView assess_type;//评测人
        public TextView assess_answer_time;//答题日期

        public ViewHoderPush(View itemView) {
            super(itemView);
            assess_answer_time = (TextView) itemView.findViewById(R.id.assess_answer_time);
            assess_type = (TextView) itemView.findViewById(R.id.assess_type);
            assess_hospitalized_status = (TextView) itemView.findViewById(R.id.assess_hospitalized_status);
            assess_dsc = (TextView) itemView.findViewById(R.id.assess_dsc);
            btn_assessrecordlist_select = (Button) itemView.findViewById(R.id.btn_assessrecordlist_select);
            ll_assessrecordlist_root = (LinearLayout) itemView.findViewById(R.id.ll_assessrecordlist_root);
            assess_title = (TextView) itemView.findViewById(R.id.assess_title);
            assess_status = (TextView) itemView.findViewById(R.id.assess_status);
            // tv_assessrecordlist_doctor = (TextView) itemView.findViewById(R.id.tv_assessrecordlist_doctor);
            assess_fraction = (TextView) itemView.findViewById(R.id.assess_fraction);
            assess_send_time = (TextView) itemView.findViewById(R.id.assess_send_time);

        }
    }
}
